package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.entity.User;
import com.sky.service.*;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private UserService userService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        // 查询指定时间段内全部订单
        List<Orders> ordersList = ordersService.list(
                new LambdaQueryWrapper<Orders>()
                        .between(Orders::getOrderTime, begin, end)
        );

        // 订单总数
        Integer totalOrderCount = ordersList.size();
        //营业额
        Double turnover = 0D;
        //有效订单数
        Integer validOrderCount = 0;
        //订单完成率
        Double orderCompletionRate = 0D;
        //平均客单价
        Double unitPrice = 0D;
        //新增用户数
        Integer newUsers = 0;

        // 遍历全部订单
        for (Orders order : ordersList) {
            // 订单有效
            if (Objects.equals(order.getStatus(), Orders.COMPLETED)) {
                // 营业额增加
                turnover = turnover + order.getAmount().doubleValue();
                // 有效订单数增加
                validOrderCount++;
            }
        }

        // 订单完成率
        if (totalOrderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount.doubleValue();
        }
        // 客单价
        if (validOrderCount != 0) {
            unitPrice = (double) (Math.round(turnover / validOrderCount * 100) / 100);
        }


        // 新增用户数
        newUsers = userService.list(
                new LambdaQueryWrapper<User>()
                        .between(User::getCreateTime, begin, end)
        ).size();


        // 返回数据
        return BusinessDataVO
                .builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    @Override
    public OrderOverViewVO getOrderOverView(LocalDateTime begin, LocalDateTime end) {

        //待接单数量
        Integer waitingOrders = ordersService.list(
                new LambdaQueryWrapper<Orders>()
                        .eq(Orders::getStatus, Orders.TO_BE_CONFIRMED)
                        .between(Orders::getOrderTime, begin, end)
        ).size();

        //待派送数量
        Integer deliveredOrders = ordersService.list(
                new LambdaQueryWrapper<Orders>()
                        .eq(Orders::getStatus, Orders.CONFIRMED)
                        .between(Orders::getOrderTime, begin, end)
        ).size();

        //已完成数量
        Integer completedOrders = ordersService.list(
                new LambdaQueryWrapper<Orders>()
                        .eq(Orders::getStatus, Orders.COMPLETED)
                        .between(Orders::getOrderTime, begin, end)
        ).size();

        //已取消数量
        Integer cancelledOrders = ordersService.list(
                new LambdaQueryWrapper<Orders>()
                        .eq(Orders::getStatus, Orders.CANCELLED)
                        .between(Orders::getOrderTime, begin, end)
        ).size();

        //全部订单
        Integer allOrders = ordersService.list(
                new LambdaQueryWrapper<Orders>()
                        .between(Orders::getOrderTime, begin, end)
        ).size();


        return OrderOverViewVO
                .builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    @Override
    public DishOverViewVO getDishOverView() {

        // 已启售数量
        Integer sold = dishService.list(
                new LambdaQueryWrapper<Dish>()
                        .eq(Dish::getStatus, StatusConstant.ENABLE)
        ).size();

        // 已停售数量
        Integer discontinued = dishService.list(
                new LambdaQueryWrapper<Dish>()
                        .eq(Dish::getStatus, StatusConstant.DISABLE)
        ).size();


        return DishOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        // 已启售数量
        Integer sold = setmealService.list(
                new LambdaQueryWrapper<Setmeal>()
                        .eq(Setmeal::getStatus, StatusConstant.ENABLE)
        ).size();

        // 已停售数量
        Integer discontinued = setmealService.list(
                new LambdaQueryWrapper<Setmeal>()
                        .eq(Setmeal::getStatus, StatusConstant.DISABLE)
        ).size();


        return SetmealOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
